// pages/mine/mine.js
Page({
  data: {
    userInfo: {
      avatar: '',
      nickname: '',
      desc: '',
      phone: ''
    },
    isLoggedIn: false
  },

  onLoad: function () {
    this.checkLoginStatus();
  },

  onShow: function () {
    this.checkLoginStatus();
  },

  checkLoginStatus: function () {
    const userInfo = wx.getStorageSync('userInfo');
    const isLoggedIn = wx.getStorageSync('isLoggedIn');
    
    if (userInfo && isLoggedIn) {
      this.setData({
        userInfo: userInfo,
        isLoggedIn: true
      });
    } else {
      this.setData({
        userInfo: {
          avatar: '',
          nickname: '请登录/注册',
          desc: '完善个人信息',
          phone: ''
        },
        isLoggedIn: false
      });
    }
  },

  // 更换头像
  changeAvatar: function () {
    if (!this.data.isLoggedIn) {
      this.showLoginTip('更换头像');
      return;
    }

    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success: (res) => {
        console.log('选择图片成功:', res);
        
        const tempFilePath = res.tempFiles[0].tempFilePath;
        
        const userInfo = {
          ...this.data.userInfo,
          avatar: tempFilePath
        };
        
        wx.setStorageSync('userInfo', userInfo);
        this.setData({
          userInfo: userInfo
        });
        
        wx.showToast({
          title: '头像更换成功',
          icon: 'success'
        });
      },
      fail: (error) => {
        console.error('选择图片失败:', error);
        wx.showToast({
          title: '选择图片失败',
          icon: 'none'
        });
      }
    });
  },

  // 编辑用户资料 - 跳转到登录页面
  editUserInfo: function () {
    if (!this.data.isLoggedIn) {
      wx.navigateTo({
        url: '/pages/login/login'
      });
    } else {
      wx.navigateTo({
        url: '/pages/editProfile/editProfile'
      });
    }
  },

  // 跳转到消息中心页面
  goToMessageCenter: function () {
    if (!this.data.isLoggedIn) {
      this.showLoginTip('查看消息');
      return;
    }
    wx.showToast({
      title: '消息中心开发中',
      icon: 'none'
    });
  },

  // 跳转到会员规则页面
  goToMembership: function () {
    wx.showToast({
      title: '会员规则开发中',
      icon: 'none'
    });
  },

  // 联系客服
  contactService: function () {
    wx.showModal({
      title: '联系客服',
      content: '客服电话：400-000-0000\n工作时间：9:00-18:00',
      confirmText: '拨打',
      success: (res) => {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: '4000000000'
          });
        }
      }
    });
  },

  // 跳转到关于我们页面
  aboutUs: function () {
    wx.showToast({
      title: '关于我们开发中',
      icon: 'none'
    });
  },

  // 退出登录
  // pages/mine/mine.js - 修改退出登录逻辑
logout: function () {
  wx.showModal({
    title: '提示',
    content: '确定要退出登录吗？',
    success: (res) => {
      if (res.confirm) {
        wx.removeStorageSync('userInfo');
        wx.removeStorageSync('isLoggedIn');
        
        // 退出后跳转到欢迎页
        wx.reLaunch({
          url: '/pages/welcome/welcome'
        });
      }
    }
  });
},

  // 显示登录提示
  showLoginTip: function (action) {
    wx.showModal({
      title: '提示',
      content: `请先登录后再${action}`,
      confirmText: '去登录',
      success: (res) => {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/login/login'
          });
        }
      }
    });
  }
})